package com.ruoyi.web.controller.system;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.ruoyi.system.domain.vo.SysWhitePaperOutlineVo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysWhitePaperOutline;
import com.ruoyi.system.service.ISysWhitePaperOutlineService;

/**
 * 白皮书大纲Controller
 *
 * @author ruoyi
 * @date 2025-10-20
 */
@RestController
@RequestMapping("/system/outline")
public class SysWhitePaperOutlineController extends BaseController {
    @Autowired
    private ISysWhitePaperOutlineService sysWhitePaperOutlineService;

    /**
     * 查询白皮书大纲列表，用于大纲页面反显
     */
    @GetMapping("/detailList")
    public AjaxResult detailList(Long projectId) {
        List<SysWhitePaperOutlineVo> outlineList = sysWhitePaperOutlineService.selectSysWhitePaperOutlineList(projectId);

        List<Map<String, Object>> whitePaperChapterList = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(outlineList)) {
            Map<Long, List<SysWhitePaperOutlineVo>> outlineMap = outlineList.stream().collect(Collectors.groupingBy(SysWhitePaperOutlineVo::getChapterSort));
            Set<Long> chapterSortSet = outlineMap.keySet();
            TreeSet<Long> chapterSortTreeSet = Sets.newTreeSet(chapterSortSet);

            for (Long chapterSort : chapterSortTreeSet) {
                List<SysWhitePaperOutlineVo> tempList = outlineMap.get(chapterSort);
                Map<String, Object> whitePaperChapterMap = Maps.newHashMap();
                whitePaperChapterMap.put("chapterName", tempList.get(0).getChapterName());
                whitePaperChapterMap.put("outlineList", tempList);

                whitePaperChapterList.add(whitePaperChapterMap);
            }
        }

        return success(whitePaperChapterList);
    }

    /**
     * 查询白皮书大纲列表，用于内容汇编页面左侧树展示
     */
    @GetMapping("/list")
    public AjaxResult list(Long projectId) {
        List<SysWhitePaperOutlineVo> outlineList = sysWhitePaperOutlineService.selectSysWhitePaperOutlineList(projectId);

        List<Map<String, Object>> resultList = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(outlineList)) {
            Map<Long, List<SysWhitePaperOutlineVo>> outlineMap = outlineList.stream().collect(Collectors.groupingBy(SysWhitePaperOutlineVo::getChapterId));
            Set<Long> chapterIdSet = outlineMap.keySet();

            for (Long chapterId : chapterIdSet) {
                List<SysWhitePaperOutlineVo> tempList = outlineMap.get(chapterId);
                List<Map<String, Object>> childrenList = Lists.newArrayList();
                for (SysWhitePaperOutlineVo outline : tempList) {
                    List<Map<String, Object>> secondChildrenList = Lists.newArrayList();
                    Map<String, Object> secondChildren = Maps.newHashMap();
                    secondChildren.put("id", outline.getId());
                    secondChildren.put("label", outline.getAiAnalyseResultTitle());
                    secondChildren.put("level", 3);
                    secondChildren.put("summary", "数据来源:" + outline.getDataSource());
                    secondChildren.put("content", outline.getAiAnalyseResult());
                    secondChildren.put("finalContent", outline.getFianlAnalyseResult());
                    secondChildren.put("createTime", outline.getCreateTime());

                    secondChildrenList.add(secondChildren);

                    Map<String, Object> children = Maps.newHashMap();
                    children.put("id", outline.getId());
                    children.put("level", 2);
                    children.put("label", outline.getTitle());
                    children.put("children", secondChildrenList);
                    childrenList.add(children);
                }
                SysWhitePaperOutlineVo temp = tempList.get(0);

                Map<String, Object> resultMap = Maps.newHashMap();
                resultMap.put("id", chapterId);
                resultMap.put("level", 1);
                resultMap.put("label", temp.getChapterName());
                resultMap.put("children", childrenList);
                resultList.add(resultMap);
            }
        }

        return success(resultList);
    }

    /**
     * 保存白皮书的汇编内容
     */
    @Log(title = "白皮书大纲", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysWhitePaperOutline sysWhitePaperOutline) throws UnsupportedEncodingException {
        String fianlAnalyseResult = URLDecoder.decode(sysWhitePaperOutline.getFianlAnalyseResult(), "UTF-8");
        sysWhitePaperOutline.setFianlAnalyseResult(fianlAnalyseResult);

        return toAjax(sysWhitePaperOutlineService.updateSysWhitePaperOutline(sysWhitePaperOutline));
    }
}
